$(function () {
    findAllList();//列出购物车
});

function findAllList() {
    var totalPrice = 0;//总价格
    var sum = 0;//总数量
    $.ajax({
        method: "post",
        url: "../findAllList.do",
        success: function (data) {
            $("#menuList").empty();
            for (var i = 0; i < data.length; i++) {
                sum = parseInt(sum) + parseInt(data[i].number);
                totalPrice = parseFloat(totalPrice) + parseInt(data[i].number) * parseFloat(data[i].fooddetail.foodPrice);
                var table = `<table class="order_list">
\t<tr>
    \t<td class="name">${data[i].fooddetail.foodName}</td><td><span class="dis_price"></span></td><td></td>
    </tr>
    <tr>
    \t<td>${data[i].fooddetail.foodPrice}/份</td><td class="discount"></td><td class="m_num"><span class="count" onclick="addNumber(${data[i].fooddetail.foodId})">+</span><span class="amount">${data[i].number}</span><span class="count" onclick="reduceNumber(${data[i].fooddetail.foodId})">-</span></td>
    </tr>
    <tr>
    \t<td class="edit" ><a href="#"><img onclick="delFood(${data[i].fooddetail.foodId})" src="../img/close.png"/></a></td><td></td><td></td>
    </tr>
</table>`;
                $("#menuList").append(table);
            }
            $("#totalFood").html(sum);
            $("#totalPrice").html(totalPrice);
            $("#totals").val(totalPrice);
        },
        dataType: "json",
        error: function () {
            alert("系统出错");
        },
        async: false
    });
}

function addNumber(i) {
    $.post("../addNumber.do", {"number": i}, function (data) {
        if (data === "success") {
            findAllList();
        } else {
            alert("系统出错");
        }
    }, "text");
}

function reduceNumber(i) {
    $.post("../reduceNumber.do", {"number": i}, function (data) {
        if (data === "success") {
            findAllList();
        } else {
            alert(data);
        }
    }, "json");
}

function delFood(foodId) {
    $.post("../delFoodById.do", {"foodId": foodId}, function (data) {
        if (data === "success") {
            findAllList();
        } else {
            alert(data);
        }
    }, "json");
}